{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 2.4 文件读写操作"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.CSV文件"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.049659Z",
     "end_time": "2024-05-08T20:52:13.509994Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "   City_Name       GDP  Population\n0   SHANGHAI  27466.15     2419.70\n1    BEIJING  24899.30     2172.90\n2  GUANGZHOU  19610.90     1350.11\n3   SHENZHEN  19492.60     1137.87\n4    TIANJIN  17885.39     1562.12\n5  CHONGQING  17558.76     3016.55\n6     SUZHOU  15475.09     1375.00\n7    CHENGDU  12170.20     1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City_Name</th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>SHANGHAI</td>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>BEIJING</td>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>GUANGZHOU</td>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>SHENZHEN</td>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>TIANJIN</td>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>CHONGQING</td>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>SUZHOU</td>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>CHENGDU</td>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp = pd.read_csv('gdp-population.csv')\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.514493Z",
     "end_time": "2024-05-08T20:52:13.555742Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['City_Name', 'GDP', 'Population'], dtype='object')"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.columns"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.539990Z",
     "end_time": "2024-05-08T20:52:13.592014Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "   City_Name       GDP  Population\n0   SHANGHAI  27466.15     2419.70\n1    BEIJING  24899.30     2172.90\n2  GUANGZHOU  19610.90     1350.11\n3   SHENZHEN  19492.60     1137.87\n4    TIANJIN  17885.39     1562.12\n5  CHONGQING  17558.76     3016.55\n6     SUZHOU  15475.09     1375.00\n7    CHENGDU  12170.20     1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City_Name</th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>SHANGHAI</td>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>BEIJING</td>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>GUANGZHOU</td>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>SHENZHEN</td>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>TIANJIN</td>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>CHONGQING</td>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>SUZHOU</td>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>CHENGDU</td>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp = pd.read_csv('gdp-population.csv', header=0)\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.559315Z",
     "end_time": "2024-05-08T20:52:13.636766Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "        CITY        GDP         POP\n0  City_Name        GDP  Population\n1   SHANGHAI  27466.15     2419.70 \n2    BEIJING  24899.30     2172.90 \n3  GUANGZHOU  19610.90     1350.11 \n4   SHENZHEN  19492.60     1137.87 \n5    TIANJIN  17885.39     1562.12 \n6  CHONGQING  17558.76     3016.55 \n7     SUZHOU  15475.09     1375.00 \n8    CHENGDU  12170.20     1591.76 ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CITY</th>\n      <th>GDP</th>\n      <th>POP</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>City_Name</td>\n      <td>GDP</td>\n      <td>Population</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>SHANGHAI</td>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>BEIJING</td>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>GUANGZHOU</td>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>SHENZHEN</td>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>TIANJIN</td>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>CHONGQING</td>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>SUZHOU</td>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>CHENGDU</td>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('gdp-population.csv', names=['CITY', 'GDP', 'POP'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.576096Z",
     "end_time": "2024-05-08T20:52:13.678233Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "        CITY       GDP      POP\n0   SHANGHAI  27466.15  2419.70\n1    BEIJING  24899.30  2172.90\n2  GUANGZHOU  19610.90  1350.11\n3   SHENZHEN  19492.60  1137.87\n4    TIANJIN  17885.39  1562.12\n5  CHONGQING  17558.76  3016.55\n6     SUZHOU  15475.09  1375.00\n7    CHENGDU  12170.20  1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>CITY</th>\n      <th>GDP</th>\n      <th>POP</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>SHANGHAI</td>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>BEIJING</td>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>GUANGZHOU</td>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>SHENZHEN</td>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>TIANJIN</td>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>CHONGQING</td>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>SUZHOU</td>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>CHENGDU</td>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('gdp-population.csv', names=['CITY', 'GDP', 'POP'], skiprows=[0])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.593013Z",
     "end_time": "2024-05-08T20:52:13.683984Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP      POP\nCITY                        \nSHANGHAI   27466.15  2419.70\nBEIJING    24899.30  2172.90\nGUANGZHOU  19610.90  1350.11\nSHENZHEN   19492.60  1137.87\nTIANJIN    17885.39  1562.12\nCHONGQING  17558.76  3016.55\nSUZHOU     15475.09  1375.00\nCHENGDU    12170.20  1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>POP</th>\n    </tr>\n    <tr>\n      <th>CITY</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp = pd.read_csv('gdp-population.csv', names=['CITY', 'GDP', 'POP'], skiprows=[0], index_col=0)\n",
    "gdp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.614384Z",
     "end_time": "2024-05-08T20:52:13.689045Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['SHANGHAI', 'BEIJING', 'GUANGZHOU', 'SHENZHEN', 'TIANJIN', 'CHONGQING',\n       'SUZHOU', 'CHENGDU'],\n      dtype='object', name='CITY')"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdp.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.633682Z",
     "end_time": "2024-05-08T20:52:13.730941Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP      POP\nCITY                        \nSHANGHAI   27466.15      NaN\nBEIJING    24899.30  2172.90\nGUANGZHOU  19610.90  1350.11\nSHENZHEN        NaN  1137.87\nTIANJIN    17885.39  1562.12\nCHONGQING  17558.76  3016.55\nSUZHOU     15475.09  1375.00\nCHENGDU    12170.20  1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>POP</th>\n    </tr>\n    <tr>\n      <th>CITY</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>NaN</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('gdp-pop.csv', names=['CITY', 'GDP', 'POP'], skiprows=[0], index_col=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.655631Z",
     "end_time": "2024-05-08T20:52:13.829282Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP      POP\nCITY                        \nSHANGHAI   27466.15      NaN\nBEIJING    24899.30  2172.90\nGUANGZHOU  19610.90  1350.11\nSHENZHEN        NaN  1137.87\nTIANJIN    17885.39  1562.12\nCHONGQING  17558.76      NaN\nSUZHOU     15475.09  1375.00\nCHENGDU    12170.20  1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>POP</th>\n    </tr>\n    <tr>\n      <th>CITY</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>NaN</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = {\"GDP\": [None]}\n",
    "pd.read_csv('gdp-pop2.csv',\n",
    "            names=['CITY', 'GDP', 'POP'], skiprows=[0], index_col=0, na_values=s)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.678732Z",
     "end_time": "2024-05-08T20:52:13.884139Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "   City_Name       GDP  Population\n0   SHANGHAI  27466.15     2419.70\n1    BEIJING  24899.30     2172.90\n2  GUANGZHOU  19610.90     1350.11",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>City_Name</th>\n      <th>GDP</th>\n      <th>Population</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>SHANGHAI</td>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>BEIJING</td>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>GUANGZHOU</td>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('gdp-population.csv', nrows=3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.709414Z",
     "end_time": "2024-05-08T20:52:13.913693Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "<pandas.io.parsers.readers.TextFileReader at 0x1102b84dcc0>"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aqi_chunk = pd.read_csv('aqi.csv', chunksize=50, encoding='utf-8', header=0)\n",
    "aqi_chunk"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.736172Z",
     "end_time": "2024-05-08T20:52:13.913693Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "级别\n中度污染      8.0\n优       105.0\n良       180.0\n轻度污染     62.0\ndtype: object"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tot = pd.Series([])\n",
    "for city in aqi_chunk:\n",
    "    tot = tot.add(city['级别'].value_counts(), fill_value=0)\n",
    "tot"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.753110Z",
     "end_time": "2024-05-08T20:52:13.918486Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.HDF5文件"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "<HDF5 file \"hdftest.hdf5\" (mode r+)>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import h5py\n",
    "\n",
    "f = h5py.File('hdftest.hdf5', 'w')\n",
    "f"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.780752Z",
     "end_time": "2024-05-08T20:52:14.009961Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "<HDF5 dataset \"dataset1\": shape (99, 99), type \"<f4\">"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = f.create_dataset(name='/dataset1', shape=(99, 99))\n",
    "d"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.863175Z",
     "end_time": "2024-05-08T20:52:14.090613Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "'/dataset1'"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.name"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.887990Z",
     "end_time": "2024-05-08T20:52:14.131077Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "(99, 99)"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.907733Z",
     "end_time": "2024-05-08T20:52:14.217075Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "dtype('<f4')"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d.dtype"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.941141Z",
     "end_time": "2024-05-08T20:52:14.261895Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       ...,\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.]], dtype=float32)"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d[:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.966250Z",
     "end_time": "2024-05-08T20:52:14.308253Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "data": {
      "text/plain": "'/group1'"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = f.create_group('group1')\n",
    "g.name"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:13.990380Z",
     "end_time": "2024-05-08T20:52:14.314018Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "d2 = g.create_dataset(name='in_group1', shape=(20,))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.015136Z",
     "end_time": "2024-05-08T20:52:14.314018Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "'/group1/in_group1'"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d2.name"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.030871Z",
     "end_time": "2024-05-08T20:52:14.363648Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "d3 = f['group1/in_group1']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.043758Z",
     "end_time": "2024-05-08T20:52:14.394266Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "<HDF5 dataset \"in_group1\": shape (20,), type \"<f4\">"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.072466Z",
     "end_time": "2024-05-08T20:52:14.394266Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "['dataset1', 'group1']"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(f.keys())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.095394Z",
     "end_time": "2024-05-08T20:52:14.434324Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "[('dataset1', <HDF5 dataset \"dataset1\": shape (99, 99), type \"<f4\">),\n ('group1', <HDF5 group \"/group1\" (1 members)>)]"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(f.items())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.115285Z",
     "end_time": "2024-05-08T20:52:14.435227Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "[<HDF5 dataset \"dataset1\": shape (99, 99), type \"<f4\">,\n <HDF5 group \"/group1\" (1 members)>]"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(f.values())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.131077Z",
     "end_time": "2024-05-08T20:52:14.477796Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [],
   "source": [
    "store = pd.HDFStore('data.h5')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:14.164403Z",
     "end_time": "2024-05-08T20:52:15.874218Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "gdp = pd.read_csv('gdp-population.csv', names=['CITY', 'GDP', 'POP'], skiprows=[0], index_col=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:15.874218Z",
     "end_time": "2024-05-08T20:52:15.895693Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "<class 'pandas.io.pytables.HDFStore'>\nFile path: data.h5"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "store['gdp'] = gdp['GDP']\n",
    "store['gdp_pop'] = gdp\n",
    "store"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:15.895693Z",
     "end_time": "2024-05-08T20:52:15.958382Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "                GDP      POP\nCITY                        \nSHANGHAI   27466.15  2419.70\nBEIJING    24899.30  2172.90\nGUANGZHOU  19610.90  1350.11\nSHENZHEN   19492.60  1137.87\nTIANJIN    17885.39  1562.12\nCHONGQING  17558.76  3016.55\nSUZHOU     15475.09  1375.00\nCHENGDU    12170.20  1591.76",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>GDP</th>\n      <th>POP</th>\n    </tr>\n    <tr>\n      <th>CITY</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>SHANGHAI</th>\n      <td>27466.15</td>\n      <td>2419.70</td>\n    </tr>\n    <tr>\n      <th>BEIJING</th>\n      <td>24899.30</td>\n      <td>2172.90</td>\n    </tr>\n    <tr>\n      <th>GUANGZHOU</th>\n      <td>19610.90</td>\n      <td>1350.11</td>\n    </tr>\n    <tr>\n      <th>SHENZHEN</th>\n      <td>19492.60</td>\n      <td>1137.87</td>\n    </tr>\n    <tr>\n      <th>TIANJIN</th>\n      <td>17885.39</td>\n      <td>1562.12</td>\n    </tr>\n    <tr>\n      <th>CHONGQING</th>\n      <td>17558.76</td>\n      <td>3016.55</td>\n    </tr>\n    <tr>\n      <th>SUZHOU</th>\n      <td>15475.09</td>\n      <td>1375.00</td>\n    </tr>\n    <tr>\n      <th>CHENGDU</th>\n      <td>12170.20</td>\n      <td>1591.76</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "store['gdp_pop']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:15.916267Z",
     "end_time": "2024-05-08T20:52:15.958882Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "store.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:15.936050Z",
     "end_time": "2024-05-08T20:52:15.958882Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-05-08T20:52:15.958882Z",
     "end_time": "2024-05-08T20:52:15.974692Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
